package com.ace3l.legal.gwt.lawfirmportal.mainpage.client;


import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.TableLayout;
import com.gwtext.client.widgets.layout.TableLayoutData;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.synesthesiaconsulting.gwt.resources.client.DirectorUnit;
import com.synesthesiaconsulting.gwt.resources.client.GWTUtils;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTLawFirm;
import com.ace3l.legal.gwt.domainmodule.client.UserServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.UserService;
import com.ace3l.legal.gwt.domainmodule.client.ProjectServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.ProjectService;


/**
 * ACE3L.COM
 * com.ace3l.legal.gwt.lawfirmportal.client
 * This code was written by Synesthesia Consulting Services in conjunction
 * with Ace3L.com. This code is confidential and proprietary. This code is
 * copyright (C) 2008  Synesthesia Consulting Services and Ace3L.com
 * Created By: ageer
 * Date: Jul 19, 2008
 * Time: 8:00:14 PM
 */
public class LawFirmPortalWidget extends Composite {
    Panel portalPanel = new Panel();
    Panel tablePanel = new Panel();

    Toolbar buttonBar = new Toolbar();

    Panel newsPanel = new Panel();
    Panel notificationPanel = new Panel();
    Panel projectsPanel = new Panel();
    DirectorUnit actionDirector;
    GWTLawFirm myLawFirm;

    public LawFirmPortalWidget(DirectorUnit director) {
        actionDirector = director;
        newsPanelInit();
        notificationPanelInit();
        projectsPanelInit();
        toolbarInit();

        portalPanel.setBorder(true);
        portalPanel.setPaddings(10);
        portalPanel.setLayout(new FitLayout());
        portalPanel.setTopToolbar(buttonBar);

        tablePanel.setAutoScroll(true);
        tablePanel.setBorder(false);
        tablePanel.setLayout(new TableLayout(2));

        tablePanel.add(new PaddedPanel(newsPanel, 0, 0, 5, 10));
        tablePanel.add(new PaddedPanel(notificationPanel, 0, 5, 0, 10));
        tablePanel.add(new PaddedPanel(projectsPanel, 0, 0, 0, 0), new TableLayoutData(2));

        portalPanel.add(tablePanel);

        initWidget(portalPanel);
    }

    private void newsPanelInit() {

        newsPanel.setWidth(464);
        newsPanel.setHeight(300);
        newsPanel.setFrame(true);
        newsPanel.setTitle("News");
        newsPanel.setCollapsible(true);
        newsPanel.setAnimCollapse(true);
        Label myLabel = new Label("News Portlet");
        newsPanel.add(myLabel);
    }

    private void notificationPanelInit() {
        notificationPanel.setWidth(464);
        notificationPanel.setHeight(300);
        notificationPanel.setFrame(true);
        notificationPanel.setTitle("Notifications");
        notificationPanel.setCollapsible(true);
        notificationPanel.setAnimCollapse(true);
        Label myLabel = new Label("Notification Portlet");
        notificationPanel.add(myLabel);
    }

    private void projectsPanelInit() {
        projectsPanel.setWidth(938);
        projectsPanel.setHeight("auto");
        projectsPanel.setFrame(true);
        projectsPanel.setTitle("My Projects");
        projectsPanel.setCollapsible(true);
        projectsPanel.setAnimCollapse(true);
         //title, description, id, hoursAllotted, hoursCompleted, workflowStage, dueDate
        final GridPanel grid = new GridPanel();
        final RecordDef recordDef = new RecordDef(new FieldDef[]{
                new StringFieldDef("title",0),
                new DateFieldDef("duedate", 6, "Y-m-d 00:00:00.0"),
                new IntegerFieldDef("completed", 4),
                new IntegerFieldDef("allotted", 3),
                new StringFieldDef("status", 5)
         });
        ColumnConfig[] columns = new ColumnConfig[] {
                new ColumnConfig("Project Title", "title", 300, true),
                new ColumnConfig("Due Date","duedate",100, true),
                new ColumnConfig("Hours Completed","completed", 100, true),
                new ColumnConfig("Hours Allotted", "allotted", 100, true),
                new ColumnConfig("Status", "status", 100, true)
        };
        grid.setColumnModel(new ColumnModel(columns));
        grid.setHeight(200);
        grid.setWidth(920);
        HttpProxy prox = new HttpProxy(GWTUtils.getUrlWithContext()+"/SQLQueryMap");

        JsonReader read = new JsonReader("list", recordDef);
        Store myStore = new Store(prox,read);
        myStore.setBaseParams(new UrlParam[] {
                new UrlParam("ACTION","projects.byid.abridged")
        });
        myStore.load();
        grid.setStore(myStore);
        projectsPanel.add(grid);
    }

    private void toolbarInit() {
        ButtonListenerAdapter baCreate = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                actionDirector.applyAction("createproject");
            }
        };
        ButtonListenerAdapter baProfile = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {

            }
        };
        final AsyncCallback logoutCallback = new AsyncCallback() {
            public void onSuccess(Object result) {
                actionDirector.applyAction("logout");
            }

            public void onFailure(Throwable caught) {
                actionDirector.applyAction("logout");
            }
        };
        ButtonListenerAdapter baLogout = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                UserServiceAsync us = UserService.App.getInstance();
                us.logoutUser(logoutCallback);
            }
        };
        final ToolbarButton createProject = new ToolbarButton("Create New Project...", baCreate);
        final ToolbarButton myProfile = new ToolbarButton("My Profile", baProfile);
        final ToolbarButton logout = new ToolbarButton("Logout", baLogout);
        AsyncCallback callback = new AsyncCallback() {
            public void onSuccess(Object result) {
                myLawFirm = (GWTLawFirm) result;
                buttonBar.addText("Welcome Back " + myLawFirm.getContactFirstName());
                buttonBar.addFill();
                buttonBar.addButton(createProject);
                buttonBar.addSeparator();
                buttonBar.addButton(myProfile);
                buttonBar.addSeparator();
                buttonBar.addButton(logout);
            }

            public void onFailure(Throwable caught) {
                buttonBar.addText("Welcome Back.");
                buttonBar.addFill();
                buttonBar.addButton(createProject);
                buttonBar.addSeparator();
                buttonBar.addButton(myProfile);
                buttonBar.addSeparator();
                buttonBar.addButton(logout);
            }
        };
        UserServiceAsync us = UserService.App.getInstance();
        us.getLawFirm(new Integer(GWTUtils.getUserId()), callback);

    }

    
}
